﻿Public Class PeriodsForm

    Private Sub PeriodsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MainSplitContainer.SplitterDistance = 200
        LoadPeriodsView()

    End Sub

    Private Sub LoadPeriodsView()
        Dim AView As DataView
        Dim Arec As DataRowView

        Dim str(2) As String
        Dim itm As ListViewItem

        Dim Sql As String

        Sql = "Select * from Periods"

        With PeriodsView
            .FullRowSelect = True
            .Items.Clear()

            AView = LoadDataView(Sql)

            For Each Arec In AView
                str(0) = Arec.Item("Period_ID").ToString
                str(1) = Arec.Item("Period_Name").ToString

                itm = New ListViewItem(str)
                .Items.Add(itm)
            Next

        End With
    End Sub

    Private Sub LoadTypesView()
        Dim AView As DataView
        Dim Arec As DataRowView

        Dim str(2) As String
        Dim itm As ListViewItem

        Dim Sql As String

        If PeriodsView.SelectedItems.Count = 0 Then Exit Sub

        Sql = "Select Payment_ID, Payment_Description from payment_types where Period_ID = " & PeriodsView.SelectedItems.Item(0).Text

        DatesView.Items.Clear()

        With TypesView
            .FullRowSelect = True
            .Items.Clear()

            AView = LoadDataView(Sql)

            For Each Arec In AView
                str(0) = Arec.Item("Payment_ID").ToString
                str(1) = Arec.Item("Payment_Description").ToString

                itm = New ListViewItem(str)
                .Items.Add(itm)
            Next

        End With
    End Sub

    Private Sub LoadDatesView()
        Dim AView As DataView
        Dim Arec As DataRowView

        Dim str As String
        Dim itm As ListViewItem

        Dim Sql As String

        If PeriodsView.SelectedItems.Count = 0 Then Exit Sub
        If TypesView.SelectedItems.Count = 0 Then Exit Sub

        Sql = "Select Payment_Date from payment_dates where Payment_ID = " & TypesView.SelectedItems.Item(0).Text


        With DatesView
            .FullRowSelect = True
            .Items.Clear()

            AView = LoadDataView(Sql)

            For Each Arec In AView
                str = FormatDateTime(Arec.Item("Payment_Date"), DateFormat.ShortDate)

                itm = New ListViewItem(str)
                .Items.Add(itm)
            Next

        End With
    End Sub

    Private Sub PeriodsView_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PeriodsView.SelectedIndexChanged

        LoadTypesView()

    End Sub

    Private Sub TypesView_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TypesView.SelectedIndexChanged
        LoadDatesView()

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to add periods." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Add Period", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        With PeriodsDialog
            .PeriodBox.Enabled = True
            .PeriodBox.Text = ""
            .StartDateBox.Value = Now
            .EndDateBox.Value = Now

        End With

        If PeriodsDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim sql As String
            Dim PeriodID As Integer

            With PeriodsDialog
                PeriodID = Mid(.PeriodBox.Text, 1, 4) & Mid(.PeriodBox.Text, 6, 2) & "1"
                sql = "insert into Periods (Period_ID, Period_Name, Start_Date, End_Date) Values(" & PeriodID & ", '" & .PeriodBox.Text & "', " & .StartDateBox.Value.Date.ToOADate - 2 & ", " & .EndDateBox.Value.Date.ToOADate - 2 & ")"

                RunNonQuery(sql)

            End With
            LoadPeriodsView()
        End If
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to edit periods." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Edit Period", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        If PeriodsView.SelectedItems.Count = 0 Then Exit Sub

        With PeriodsDialog
            Dim sql As String
            Dim AView As DataView
            sql = "Select * from periods where period_ID = " & PeriodsView.SelectedItems.Item(0).Text

            AView = LoadDataView(sql)

            .PeriodBox.Text = AView(0).Item("Period_Name").ToString
            .StartDateBox.Value = AView(0).Item("Start_Date")
            .EndDateBox.Value = AView(0).Item("End_Date")
            .PeriodBox.Enabled = False

        End With

        If PeriodsDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim sql As String
            Dim PeriodID As Integer

            With PeriodsDialog
                PeriodID = Mid(.PeriodBox.Text, 1, 4) & Mid(.PeriodBox.Text, 6, 2) & "1"
                sql = "Update Periods set Start_Date = " & .StartDateBox.Value.Date.ToOADate - 2 & ", End_Date = " & .EndDateBox.Value.Date.ToOADate - 2 & " where Period_ID = " & PeriodID

                ' RunNonQuery(sql)

            End With
            LoadPeriodsView()
        End If
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to delete periods." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Delete Period", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        Dim sql As String

        If PeriodsView.SelectedItems.Count = 0 Then Exit Sub

        'check to see if there are any payment dates attached to this period
        sql = "select * from Payment_Types where Period_ID = " & PeriodsView.SelectedItems.Item(0).Text

        If LoadDataView(sql).Count = 0 Then
            If MessageBox.Show("Are you sure you want to delete this period?", "Delete period", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                RunNonQuery("Delete from Periods where Period_ID = " & PeriodsView.SelectedItems.Item(0).Text)
                LoadPeriodsView()
            End If
        Else
            MessageBox.Show("This period cannot be deleted. There are payment dates attached to this period", "Cannot delete period", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

    End Sub


    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to add payment types." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Add Payment Type", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        If PeriodsView.SelectedItems.Count = 0 Then Exit Sub

        With PaymentDatesDialog
            .Text = "Payment Type for " & PeriodsView.SelectedItems.Item(0).SubItems(1).Text
            .TypeBox.SelectedIndex = 0
            .DatesList.Items.Clear()
            .DescriptionBox.Text = ""

            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                'add the records
                'Payment Type

                Dim sql As String
                Dim PaymentID As String
                Dim counter As String
                Dim PaymentType As String

                sql = "select count(*) + 1 as counter from Payment_Types where period_id = " & PeriodsView.SelectedItems.Item(0).Text

                counter = LoadDataView(sql)(0).Item("counter").ToString

                If Len(counter) = 1 Then
                    counter = "0" & counter
                End If

                If .TypeBox.SelectedIndex = 0 Then
                    PaymentType = "A"
                Else
                    PaymentType = "F"
                End If

                PaymentID = PeriodsView.SelectedItems.Item(0).Text & counter

                sql = "insert into Payment_types (Period_ID, Payment_ID, Payment_Type, Payment_Description) values(" & PeriodsView.SelectedItems.Item(0).Text & ", " & _
                        PaymentID & ", '" & PaymentType & "', '" & .DescriptionBox.Text & "')"

                RunNonQuery(sql)

                'save the dates

                Dim r As Integer

                For r = 0 To .DatesList.Items.Count - 1

                    sql = "insert into Payment_Dates (Payment_ID, Payment_Date) values (" & PaymentID & "," & CDate(.DatesList.Items(r).Text).ToOADate - 2 & ")"
                    RunNonQuery(sql)
                Next
                LoadTypesView()

            End If
        End With

    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to edit payemnt types." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Edit Payment Type", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        If PeriodsView.SelectedItems.Count = 0 Then Exit Sub
        If TypesView.SelectedItems.Count = 0 Then Exit Sub

        With PaymentDatesDialog
            .Text = "Payment Type for " & PeriodsView.SelectedItems.Item(0).SubItems(1).Text

            Dim sql2 As String

            'get type
            sql2 = "select * from Payment_Types where Payment_ID = " & TypesView.SelectedItems.Item(0).Text


            If LoadDataView(sql2)(0).Item("Payment_Type").ToString = "A" Then
                .TypeBox.SelectedIndex = 0
            Else
                .TypeBox.SelectedIndex = 1
            End If

            .DescriptionBox.Text = TypesView.SelectedItems.Item(0).SubItems(1).Text

            'load the dates
            .DatesList.Items.Clear()
            Dim c As Integer

            For c = 0 To DatesView.Items.Count - 1
                .DatesList.Items.Add(DatesView.Items(c).Text)
            Next


            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                'add the records
                'Payment Type

                Dim sql As String
                Dim PaymentID As String
                Dim counter As String
                Dim PaymentType As String




                If .TypeBox.SelectedIndex = 0 Then
                    PaymentType = "A"
                Else
                    PaymentType = "F"
                End If

                PaymentID = TypesView.SelectedItems.Item(0).Text

                'first delete
                sql = "delete from payment_types where Payment_ID = " & PaymentID
                RunNonQuery(sql)

                sql = "insert into Payment_types (Period_ID, Payment_ID, Payment_Type, Payment_Description) values(" & PeriodsView.SelectedItems.Item(0).Text & ", " & _
                        PaymentID & ", '" & PaymentType & "', '" & .DescriptionBox.Text & "')"

                RunNonQuery(sql)

                'save the dates

                sql = "delete from payment_dates where Payment_ID = " & PaymentID
                RunNonQuery(sql)

                Dim r As Integer

                For r = 0 To .DatesList.Items.Count - 1

                    sql = "insert into Payment_Dates (Payment_ID, Payment_Date) values (" & PaymentID & "," & CDate(.DatesList.Items(r).Text).ToOADate - 2 & ")"
                    RunNonQuery(sql)
                Next
                LoadTypesView()

            End If
        End With
    End Sub

    Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to delete payment types." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Delete Payment Type", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        Dim sql As String

        If TypesView.SelectedItems.Count = 0 Then Exit Sub

        'check to see if there are any payment dates attached to this period
        sql = "select * from Pattern_IDs where Payment_ID = " & TypesView.SelectedItems.Item(0).Text

        If LoadDataView(sql).Count = 0 Then
            If MessageBox.Show("Are you sure you want to delete this period?", "Delete period", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                RunNonQuery("Delete from Payment_types where Payment_ID = " & TypesView.SelectedItems.Item(0).Text)
                RunNonQuery("Delete from Payment_dates where Payment_ID = " & TypesView.SelectedItems.Item(0).Text)

                LoadTypesView()

            End If
        Else
            MessageBox.Show("This payment type cannot be deleted. There are patterns attached to this type", "Cannot delete type", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub PeriodsForm_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        If UserFormCheck(Me.Name) = "N" Then
            Me.Close()
            MessageBox.Show("Currently you do not have permission to access this form." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk if you need access.", Me.Name & " Access", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
    End Sub
End Class